package com.fengdi.everyday.easy;


/**
 * @Description 3354 使数组元素等于0
 * @Date 15:04
 */
public class CountValidSelections {
    public int countValidSelections(int[] nums) {
        int sum = 0, leftSum = 0, rightSum = 0, ans = 0;
        for(int num : nums) {
            sum += num;
        }

        rightSum = sum;

        for (int num : nums) {
            if (num == 0) {
                // 先判断左边
                if (leftSum - rightSum >= 0 && leftSum - rightSum <= 1) ans++;
                // 再判断右边
                if (rightSum - leftSum >= 0 && rightSum - leftSum <= 1) ans++;
            }else {
                leftSum += num;
                rightSum -= num;
            }
        }

        return ans;
    }
}
